/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package source;

import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.image.*;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.*;

/**
 *
 * @author admin
 */
public class Game extends Canvas implements Runnable {
	public static int WIDTH = 400; //ширина
	public static int HEIGHT = 300; //высота
	public static int X = 320;
	public static int Y = 280;
	public static String NAME = "TUTORIAL 1"; //заголовок окна
    private static final long serialVersionUID = 1L;
	public boolean bRunning = false;
	public static Sprite hero;
	private boolean leftPressed = false;
	private boolean rightPressed = false;
	private boolean upPressed = false;
	private boolean downPressed = false;
	private static int x = 0;
	private static int y = 0; 

	public void run() {
		long lastTime = System.currentTimeMillis();
		long delta;

		init();

		while (bRunning) {
			delta = System.currentTimeMillis() - lastTime;
			lastTime = System.currentTimeMillis();
			update(delta);
			render();
		}
	}

	public void init() {
		addKeyListener(new KeyInputHandler());
		hero = getSprite("Sprite.bmp");
	}

	public void render() {
		BufferStrategy bs = getBufferStrategy();
		if (bs == null) {
			createBufferStrategy(2); //создаем BufferStrategy для нашего холста
			requestFocus();
			return;
		}

		float[] f = null;
		f = Color.RGBtoHSB(255, 0, 255, f);
		
		Graphics g = bs.getDrawGraphics(); //получаем Graphics из созданной нами BufferStrategy
		g.setColor(Color.getHSBColor(f[0], f[1], f[2])); //выбрать цвет
		g.fillRect(0, 0, getWidth(), getHeight()); //заполнить прямоугольник 
		hero.draw(g, x, y);
		g.dispose();
		bs.show(); //показать
	}

	public Sprite getSprite(String path) {
		BufferedImage sourceImage = null;

		try {
			URL url = this.getClass().getClassLoader().getResource(path);
			sourceImage = ImageIO.read(url);
		} catch (IOException e) {
			e.printStackTrace();
		}

		Sprite sprite = new Sprite(Toolkit.getDefaultToolkit().createImage(sourceImage.getSource()));

		return sprite;
	}

	public void update(long delta) {
		if(leftPressed == true) {
			if(x > 0) {
				x--;
			}
		}
		if(rightPressed == true) {
			if(x < WIDTH) {
				x++;
			}
		}
		if(upPressed == true) {
			if(y > 0) {
				y--;
			}
		}
		if(downPressed == true) {
			if(y < HEIGHT) {
				y++;
			}
		}
	}

	public static void main(String[] args) {
		Game game = new Game();
		game.setPreferredSize(new Dimension(WIDTH, HEIGHT));

		JFrame frame = new JFrame(Game.NAME);
		frame.setLocation(X, Y);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //выход из приложения по нажатию клавиши ESC
		frame.setLayout(new BorderLayout());
		frame.add(game, BorderLayout.CENTER); //добавляем холст на наш фрейм
		frame.pack();
		frame.setResizable(false);
		frame.setVisible(true);

		game.start();
	}
	
	public void start() {
		bRunning = true;
		new Thread(this).start();
	}
	
	private class KeyInputHandler extends KeyAdapter {
		public void keyPressed(KeyEvent e) { //клавиша нажата
			if (e.getKeyCode() == KeyEvent.VK_LEFT) {
				leftPressed = true;
			}
			if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
				rightPressed = true;
			}
			if (e.getKeyCode() == KeyEvent.VK_UP) {
				upPressed = true;
			}
			if (e.getKeyCode() == KeyEvent.VK_DOWN) {
				downPressed = true;
			}
		}

		public void keyReleased(KeyEvent e) { //клавиша отпущена
			if (e.getKeyCode() == KeyEvent.VK_LEFT) {
				leftPressed = false;
			}
			if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
				rightPressed = false;
			}
			if (e.getKeyCode() == KeyEvent.VK_UP) {
				upPressed = false;
			}
			if (e.getKeyCode() == KeyEvent.VK_DOWN) {
				downPressed = false;
			}
		}
	}
}

